<?php
require_once ('config.php');
?>

<html>
<head>
<title>To Book Ticket Online Please Fill Up This Form.</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />

</head>
</html>

<?php
include_once ("common.php");
printHeader();

echo "<h1>Buy movie tix</h1>";

$connect = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Could not connect: ' . mysql_error());

mysql_select_db(DB_DATABASE, $connect);
  
if ($_POST[numtix] != NULL) {  
  $sql = "update showtimes set available_tickets=available_tickets-$_POST[numtix] where movieID = (select ID from movielist where name='$_POST[movie]') and showtime = '$_POST[time]'";
  if (!mysql_query($sql)) {
  	die("Error: could not buy tickets");
  }
  exit("<center>Bought $_POST[numtix] tickets for '$_POST[movie]' @$_POST[time]</center>");
} else {
  $sql = "select s.available_tickets from showtimes s, movielist m where m.ID=s.movieID and m.name = '$_GET[movie]' and s.showtime = '$_GET[time]'";
  
  print_r($_GET);
  $available_tix = mysql_fetch_row(mysql_query($sql));
  $available_tix = intval($available_tix[0]);
}
?>

<form name=form1 method=post action="buy_ticket.php" enctype="multipart/form-data">
<table align="center" id="two">
  <tr>
    <td><input type="radio" name="movie" value="<?php echo $_GET[movie]?>" checked><?php echo $_GET[movie]?></td>
    <td><input type="radio" name="time" value="<?php echo $_GET[time]?>" checked><?php echo "@$_GET[time]"?></td>
  </tr>
  <tr>
    <td>Number of tickets:
      <select name=numtix>
        <?php
          for($i = 1; $i <= $available_tix; $i++) {
          	echo "<option value='$i'> $i";
          }
         ?>
      </select>
    </td>
  </tr>
  <tr>
      <td><input type="submit" value="Buy"></td>
  </tr>
  
</table>
</form>
  